package com.byl.common.modules.controller.web;

import com.byl.common.context.ThymeleafContext;
import com.byl.sdk.bean.Response;
import com.byl.sdk.bean.ResponsePage;
import com.byl.sdk.bean.ResultBean;
import com.byl.sdk.gson.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/12/17.
 */
@Controller
public class BaseWebController {

    private static final int SUCCESS_CODE = 200;  //成功
    private static final int FAIL_CODE = 0; //错误


    /**
     * 处理成功
     *
     * @param msg 成功消息
     * @param obj 成功数据
     * @return 返回json字符串
     */
    protected String success(String msg, Object obj) {
        return GsonUtils.beanToJson(new Response(SUCCESS_CODE, msg, obj));
    }

    /**
     * 分页结果封装
     *
     * @param msg
     * @param bean
     * @return
     */
    protected String success(String msg, ResultBean bean) {
        return GsonUtils.beanToJson(new ResponsePage(SUCCESS_CODE, msg, bean.getData(), bean.getPage(), bean.getPageSize(), bean.getTotal()));
    }

    /**
     * 处理失败
     *
     * @param msg 错误消息
     * @return 返回的json字符串
     */
    protected String fail(String msg) {
        return GsonUtils.beanToJson(new Response(FAIL_CODE, msg));
    }

    /**
     * 处理失败
     *
     * @param msg 错误消息
     * @param obj 错误数据或者提示错误信息
     * @return 返回json字符串
     */
    protected String fail(String msg, Object obj) {
        return GsonUtils.beanToJson(new Response(FAIL_CODE, msg, obj));
    }


    @Value("${spring.serverurl}")
    String serverurl;

    @Value("${spring.thymeleaf.global_context}")
     String global_context;


    public String getServerurl() {
        if(null==serverurl){
            this.serverurl="http://localhost";
        }
        return serverurl;
    }

    public String getGlobal_context() {
        if(null==global_context){
            global_context= ThymeleafContext.tms.toString();
        }
        return global_context;
    }


}
